﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MediaDefinitionAuditRepository.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the MediaDefinitionAuditRepository type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DocProcessingRepository.Repositories
{
  using System;
  using System.Collections.Generic;
  using System.Data.SqlClient;
  using System.Linq;
  using DocProcessingRepository.Contexts;
  using DocProcessingRepository.Interfaces;
  using EFRepository;
  using Entities;

  public class MediaDefinitionAuditRepository : BaseEfRepository<MediaDefinitionAudit>, IMediaDefinitionAuditRepository
  {
    public MediaDefinitionAuditRepository(String connectionString)
      : base(new IdentityDb(connectionString))
    {
    }

    public List<MediaDefinitionAuditEntity> GetAudits(Int32 mediaDefinitionId)
    {
      List<MediaDefinitionAuditEntityInternal> audits = this.GetEntityListByStoreProcedure<MediaDefinitionAuditEntityInternal>(
        "GetMediaDefinitionAudits @MediaDefinitionId",
        new SqlParameter("@MediaDefinitionId", mediaDefinitionId)).ToList();

      List<MediaDefinitionAuditEntity> typedAudits = audits.Select(this.TranslateFromInternal).ToList();

      return typedAudits;
    }

    public MediaDefinitionAuditEntity AddAudit(Int32 mediaDefinitionId, String userId, MediaDefinitionAudit.MediaDefinitionAuditType auditType, DateTime date, String details)
    {
      MediaDefinitionAuditEntityInternal audit =
        this.GetEntityListByStoreProcedure<MediaDefinitionAuditEntityInternal>(
          "CreateMediaDefinitionAudit @MediaDefinitionId, @UserId, @AuditType, @AuditDate, @Details",
          new SqlParameter("@MediaDefinitionId", mediaDefinitionId),
          new SqlParameter("@UserId", userId),
          new SqlParameter("@AuditType", (Int32)auditType),
          new SqlParameter("@AuditDate", date),
          new SqlParameter("@Details", details)).Single();

      MediaDefinitionAuditEntity typedAudit = this.TranslateFromInternal(audit);

      return typedAudit;
    }

    #region Privates

    private MediaDefinitionAuditEntity TranslateFromInternal(MediaDefinitionAuditEntityInternal internalEntity)
    {
      return new MediaDefinitionAuditEntity
               {
                 MediaDefinitionAuditId = internalEntity.MediaDefinitionAuditId,
                 MediaDefinitionId = internalEntity.MediaDefinitionId,
                 UserId = internalEntity.UserId,
                 AuditType =
                   (MediaDefinitionAudit.MediaDefinitionAuditType)internalEntity.AuditType,
                 AuditDate = internalEntity.AuditDate,
                 Details = internalEntity.Details,
                 ManCoDocId = internalEntity.ManCoDocId,
                 IndexDefinitionId = internalEntity.IndexDefinitionId,
                 QueueName = internalEntity.QueueName,
                 Username = internalEntity.Username,
                 FirstName = internalEntity.FirstName,
                 LastName = internalEntity.LastName
               };
    }

    // This is required because EntityFramework cannot seem to convert data to an enum when the data is retrieved via GetEntityListByStoreProcedure
    private class MediaDefinitionAuditEntityInternal
    {
      public Int32 MediaDefinitionAuditId { get; set; }

      public Int32 MediaDefinitionId { get; set; }

      public String UserId { get; set; }

      public Int16 AuditType { get; set; }

      public DateTime AuditDate { get; set; }

      public String Details { get; set; }

      public Int32 ManCoDocId { get; set; }

      public Int32 IndexDefinitionId { get; set; }

      public String QueueName { get; set; }

      public String Username { get; set; }

      public String FirstName { get; set; }

      public String LastName { get; set; }
    }

    #endregion
  }
}
